#include "SendFileHandler.h"
#include "receivefile.h"
#include "NetworkCommonFunction.h"

#include <QDebug>
#include <boost/scoped_array.hpp>

bool SendFileHandler(int socket, int commandLength)
{
	qDebug()<<"Recieved send file command";

	const int bufferSize = 500 * 1024;
	char* buffer = new char[bufferSize];
	boost::scoped_array<char> bufferContainer(buffer);

	unsigned long transferId;
	if(!getFourByte(socket, transferId))
	{
		qDebug()<<"Get transfer id error";
		return false;
	}

	commandLength -= 4;
	if(!receiveData(socket, buffer, commandLength))
	{
		qDebug()<<"Receive file name error";
		return false;
	}
	buffer[commandLength] = 0;
	ReceiveFile receiveFile(buffer, transferId);
	receiveFile.doCommand();

	return true;
}
